//
//  CommunityViewController.m
//  LNA
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "CommunityViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "CommunityCollectionViewCell.h"
#import "CommunityModel.h"
#import "GroupModel.h"
#import "SecondViewController.h"
#import "CollectionReusableView.h"
@interface CommunityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArr;// 分区里的数组
@property (nonatomic, retain) NSMutableArray *sectionArr;// 分区数组
@property (nonatomic, retain) NSMutableArray *titleArr; // 分区头上的穷游7大洲;
@end




@implementation CommunityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // cell大小
    layout.itemSize = CGSizeMake((375 - 40)/ 2, 50);
    // 行列间距
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    // 边界
    layout.sectionInset = UIEdgeInsetsMake(10, 15, 5, 5);
    
    self.collectionView  = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    // 边距
    _collectionView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 头 垂直滚动时 高度设置生效 水平滚动时 宽度设置生效
    layout.headerReferenceSize = CGSizeMake(100, 50);

    
    [layout release];
    [_collectionView release];
    // 调用 数据处理方法
    [self dataHandler];
    [self.view addSubview:_collectionView];
    
    self.titleArr = [@[@"穷游亚洲",@"穷游欧洲",@"穷游美洲",@"穷游大洋洲",@"穷游非洲",@"穷游兴趣小组",@"线下线上",@"后院"]mutableCopy];
    
    
    // 重用池
    [_collectionView registerClass:[CommunityCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
#pragma mark - 头部区域使用
    // 参数1:注册的视图类
    // 参数2:注册类型(头部,脚部)
    // 参数3:重用标识
    
    // 头部
    [_collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];


    
}
- (void)dataHandler
{
    NSString *str = @"http://open.qyer.com/qyer/bbs/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&v=1";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        NSLog(@"%lld",downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 数据处理
        NSArray *arr = responseObject[@"data"][@"forum_list"];
        self.sectionArr = [CommunityModel modelHanderWithArrar:arr];
        [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
}
// 设置头部和脚部区域
// 参数1:响应的collectionView
// 参数2:类型
// 参数3:位置信息
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 通过kind类型判断 设置相应内容
  
        // 从重用池获取头部视图
        CollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    header.headerLabel.text = _titleArr[indexPath.section];
    header.headerLabel.font = [UIFont systemFontOfSize:14];
        return header;
}
#pragma mark - cell设置
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommunityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *arr = [_sectionArr[indexPath.section] group];
    self.dataArr = [GroupModel modelHanderWithArrar:arr];
    GroupModel *m = _dataArr[indexPath.item ];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]];
    cell.titleLabel.text = m.name;
    cell.tieziLabel.text = [NSString stringWithFormat:@"%@个帖子", m.total_threads];
    return cell;
    
}
#pragma mark - 分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return _sectionArr.count;
}
#pragma mark - item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return [[_sectionArr[section] group] count];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    SecondViewController *secVC = [[SecondViewController alloc] init];
    
    [self.navigationController pushViewController:secVC animated:YES];
        
    [secVC release];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
